﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Templates_MineHome_Default : Acms.AcmsControls.TemplatePage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        
        dsChildren.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsChildren_SelectData);
        
        dlNav.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNav_ItemDataBound);

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    public override void DataBind()
    {
        base.DataBind();
        var post = AcmsContext.CurrentPost;

        if (post != null)
        {
            //  var pnlItem = pnlItem;

            if (pnlItem != null)
            {
                var HeroImage = post.FindPostControl("HeroImage");// as ImageContentControlDisplay;
                if (HeroImage != null)
                {
                    var prop = HeroImage.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                    if (prop != null && prop.HasResource())
                    {
                        pnlItem.BackImageUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, this.Page);
                    }
                }
            }

        }
    } 

    void dsChildren_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(Post.GetAllBySiteIdAndParentPath(AcmsContext.SiteId, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion));
    }

    void dlNav_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNav") as HyperLink;
            
            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;

            }
        }
    }

}
